package cn.kzgg.org.service.impl;

import cn.kzgg.basic.exception.BusinessException;
import cn.kzgg.basic.service.impl.BaseServiceImpl;
import cn.kzgg.basic.util.BaiduAuditUtils;
import cn.kzgg.org.domain.Employee;
import cn.kzgg.org.domain.Shop;
import cn.kzgg.org.domain.ShopAuditLog;
import cn.kzgg.org.mapper.EmployeeMapper;
import cn.kzgg.org.mapper.ShopAuditLogMapper;
import cn.kzgg.org.mapper.ShopMapper;
import cn.kzgg.org.service.IEmployeeService;
import cn.kzgg.org.service.IShopService;
import cn.kzgg.org.vo.ShopVo;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author KZGG
 * @since 2022-06-12
 */

/**
 * 店铺入驻逻辑
 * 一：参数空校验
 * 。。不能为空
 * 二：合法校验   @TODO 百度AI智能审核接口做
 * <p>
 * 三：判断是否曾经已经注册过
 * 1. 店铺不能被注册
 * 2.人是否被注册
 * 2.1 注册了但是没有绑定店铺
 * 2.2 注册了并且已经绑定了店铺
 * <p>
 * 四：保存数据
 * shop
 * employee  shopId
 * <p>
 * 反过来修改shop中的admin_id
 *
 * @param shop
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 店铺入驻逻辑
     *
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        //创建管理员对象
        Employee admin = shop.getAdmin();
        //一.参数不能为空，前端校验有做判断后端也需要校验,判断店名，地址管理员姓名是否为空
        if (StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(admin.getUsername())
        ) {
            //如果是空的就提示
            throw new BusinessException("参数不能为空");
        }
        //二.合法校验
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinessException("写啥呢");
        }

        //三.判断是否注册过
        //1判断店铺是否被注册过,
        Shop shopTemp = shopMapper.loadByName(shop.getName());
        if (shopTemp != null) {//如果有这个名字
            //提示店铺已被注册
            throw new BusinessException("店铺已经存在，请直接登录");
        }
        //2判断管理员是否注册过
        Employee empTemp = employeeMapper.loadByUsername(admin.getUsername());
        //2.1有注册过并且绑定了店铺
        if (empTemp != null && empTemp.getShop_id() != null) {
            //提示信息
            throw new BusinessException("该用户已存在已经存在，请直接登录");
        }
        //2.2注册了但是没绑定店铺，直接绑定
        if (empTemp == null) {
            employeeMapper.save(admin);
            //返回自增ID
            employeeService.save(admin);
            empTemp = admin;
        }
        //四.保存数据
        //1.首先保存shop,shop对应的emp的id
        shop.setAdminId(empTemp.getId());
        shopMapper.save(shop);
        //2.反过来修改shop中的admin_id
        empTemp.setShop_id(shop.getId());
        employeeMapper.update(empTemp);
    }

    /**
     * 审核通过
     *
     * @param shopAuditLog
     */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //1.修改店铺状态(domain中设置状态)
        //1.1获取shopId
        Long shopId = shopAuditLog.getShopId();
        //1.2根据shopId拿到店铺
        Shop shop = shopMapper.loadById(shopId);
        //1.3设置状态
        shop.setState(0);
        //1.4修改对应的shop状态
        shopMapper.update(shop);

        //2.记录审核通过日志
        shopAuditLog.setNote("同意");
        shopAuditLog.setState(0);
        shopAuditLog.setAuditId(334L);
        shopAuditLog.setAuditTime(new Date());
        shopAuditLogMapper.save(shopAuditLog);

       /* //3.发送激活邮件
        //3.1发简单的激活邮件
        SimpleMailMessage message = new SimpleMailMessage();
        System.out.println(message);
        //3.2从哪儿发？从开通token的邮箱
        message.setFrom("2621473946@qq.com");
        //3.3发给谁？发给店主,根据店id查询到店长(员工)
        Employee employee = employeeMapper.loadByShopId(shopAuditLog.getShopId());
        System.out.println(employee);
        //店主的邮箱
        message.setTo(employee.getEmail());
        // System.out.println(message);
        message.setSubject("宠物乐园-店铺入驻激活请求");
        message.setText("亲，您的店铺信息已经通过审核，请尽快完成激活并使用：" +
                "激活链接：http://localhost:8080/shop/activation/" + shopAuditLog.getShopId());
        javaMailSender.send(message);*/
    }

    /**
     * 审核驳回
     *
     * @param shopAuditLog
     */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
//1.1获取shopId
        Long shopId = shopAuditLog.getShopId();
        //1.2根据shopId拿到店铺
        Shop shop = shopMapper.loadById(shopId);
        //1.3设置状态
        shop.setState(-2);
        //1.4修改对应的shop状态
        shopMapper.update(shop);

        //2.记录审核通过日志
        shopAuditLog.setState(-2);
        shopAuditLog.setNote("驳回");
        shopAuditLog.setAuditId(334L);
        shopAuditLog.setAuditTime(new Date());
        shopAuditLogMapper.save(shopAuditLog);

        //3.发送激活邮件
        //3.1发简单的激活邮件
        /*SimpleMailMessage message = new SimpleMailMessage();

        //3.2从哪儿发？从开通token的邮箱
        message.setFrom("2621473946@qq.com");
        //3.3发给谁？发给店主,根据店id
        Employee employee = employeeMapper.loadByShopId(shopAuditLog.getShopId());
        //System.out.println(message);
        //店主的邮箱
        message.setTo(employee.getEmail());
        message.setSubject("宠物乐园-店铺入驻驳回任务");
        message.setText("亲，您的店铺信息有误，审核不通过，请尽快修改并重新提交审核！！修改链接：" +
                "http://localhost:8081/#/register2?shopId=" + shopAuditLog.getShopId());
        javaMailSender.send(message);*/
    }

    /**
     * 展示报表
     * @return
     */
    @Override
    public List<ShopVo> queryEcharts() {
        return shopMapper.queryEcharts();
    }
}
